package com.mazaiting.redeye.api

import com.mazaiting.easy.base.bean.BaseBean
import com.mazaiting.easy.base.bean.Response
import com.mazaiting.easy.config.Constant
import com.mazaiting.redeye.bean.*
import io.reactivex.Observable
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

/**
 * 用户 api
 */
class UserApi constructor(private val service: UserApiService) {

    companion object {

        private var sApi: UserApi? = null

        /**
         * 单例
         * @param service 服务
         * @return api对象
         */
        fun getInstance(service: UserApiService): UserApi {
            if (null == sApi) {
                synchronized(UserApi::class.java) {
                    if (null == sApi) {
                        sApi = UserApi(service)
                    }
                }
            }
            return sApi!!
        }
    }

    /**
     * 获取公钥
     * @return 通知
     */
    fun publicKey(): Observable<BaseBean> {
        return service.publicKey()
    }

    /**
     * 登陆
     * @param user 用户信息
     * @return 通知
     */
    fun login(user: UserBean): Observable<Response<Request<UserBean>>> {
        //    RequestBody body = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        //    MultipartBody.Part image_file = MultipartBody.Part.createFormData("image_file", file.getName(), body);
//        val userName = MultipartBody.Part.createFormData("userName", username)
//        val pwd = MultipartBody.Part.createFormData("password", password)
        return service.login(user)
    }

    /**
     * 注册
     * @param user 用户信息
     */
    fun register(user: UserBean): Observable<BaseBean> {
        return service.register(user)
    }

    /**
     * 上传图片
     * @param imagePath 图片路径
     */
    fun uploadImage(imagePath: String): Observable<Response<List<ImageBean>>> {
        // 创建文件
        val file = File(imagePath)
        // 创建请求体
        val body = RequestBody.create(MediaType.parse(Constant.MULTIPART_FORM_DATA), file)
        // 创建多类型请求体
        val filePart = MultipartBody.Part.createFormData("file", file.name, body)
        return service.uploadImage(filePart)
    }

    /**
     * 用户信息
     * @param requestBean 用户信息
     */
    fun information(requestBean: Request<UserBean>): Observable<Response<UserBean>> = service.information(requestBean)

    /**
     * 更新用户信息
     * @param requestBean 请求体
     */
    fun updateUser(requestBean: Request<UserBean>): Observable<BaseBean> = service.updateUser(requestBean)

    /**
     * 更新用户信息
     * @param requestBean 请求体
     */
    fun updatePhone(requestBean: Request<VerifyCodeBean<UserBean>>): Observable<BaseBean> {
        return service.updatePhone(requestBean)
    }


    /**
     * 获取偏好列表
     * @param request 请求
     */
    fun feed(request: Request<Any>): Observable<Response<List<FeedBean>>> {
        return service.feed(request)
    }

    /**
     * 获取 Tab 列表
     * @param request 请求
     */
    fun mediaList(request: Request<Any>): Observable<Response<List<String>>> {
        return service.mediaList(request)
    }

    /**
     * 测试
     */
    fun hello(token: String): Observable<BaseBean> = service.hello(Request(token))

}
